<?php

class AdminDashboardController extends \BaseController {
	
	/**
	 * Instantiate a new AdminDashboardController instance.
	 */
	public function __construct()
	{
		$this->beforeFilter('adminAuth', ['except' => ['getLogin', 'postLogin']]);
		$this->beforeFilter('adminResourcesAuth', [
			'except' => ['getLogin', 'postLogin', 'getLogout']
		]);
	}
	
	public function getIndex()
	{
		return View::make("admins.dashboards.index");
	}
	
	public function getLogin()
	{
		return View::make("admins.dashboards.login");
	}
	
	public function postLogin()
	{
		$validator =  Validator::make(Input::all(), [
			"email" => "required|email",
			"password" => "required|alpha_num|between:5,12"
		]);
	
		if ($validator->passes()) {
			$credentials = [
				"email" => Input::get("email"),
				"password" => Input::get("password")
			];
	
			if (Auth::attempt($credentials)) {
				return Redirect::to('admin');
			}
	
			return Redirect::back()->withErrors([
				"password" => ["Credentials invalid."]
			]);
		}
	
		return Redirect::back()
			->withInput()
			->withErrors($validator);
	}
	
	public function getLogout()
	{
		Auth::logout();
		return Redirect::to('admin/login');
	}
}
